package com.ika.client.widgets.titlebar;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.ika.client.HistoryManager;
import com.ika.client.VideoService;
import com.ika.client.VideoServiceAsync;
import com.ika.client.model.LoginInfo;
import com.ika.client.model.Video;
import com.ika.client.service.LoginService;
import com.ika.client.service.LoginServiceAsync;
import com.ika.client.widgets.videolist.VideoListWidget;

public class TitleBarWidget extends Composite {

	interface Style extends CssResource {

	}

	private final static VideoServiceAsync service = GWT
			.create(VideoService.class);

	LoginInfo loginInfo = null;

	@UiField
	FlowPanel mainPanel;
	@UiField
	Label homeLabel;
	@UiField
	Label animeLabel;
	@UiField
	Label movieLabel;
	@UiField
	Label musicLabel;

	VideoListWidget videoListWidget = new VideoListWidget();

	interface TitleBarUiBinder extends UiBinder<Widget, TitleBarWidget> {
	}

	private static TitleBarUiBinder titleBarUiBinder = GWT
			.create(TitleBarUiBinder.class);

	public TitleBarWidget() {

		initWidget(titleBarUiBinder.createAndBindUi(this));

	}

	@UiHandler("animeLabel")
	public void clickOnAnimeLabel(ClickEvent event) {

		HistoryManager.putTitle(HistoryManager.ANIME);
		HistoryManager.addHistoryToken();
	}

	@UiHandler("musicLabel")
	public void clickOnMusicLabel(ClickEvent event) {
		animeLabel.setText("change");
	}

	@UiHandler("movieLabel")
	public void clickOnMovieLabel(ClickEvent event) {
		animeLabel.setText("change");
	}

	public void playVideo() {
		mainPanel.add(videoListWidget);

		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(),
				new AsyncCallback<LoginInfo>() {
					public void onFailure(Throwable error) {
					}

					public void onSuccess(LoginInfo result) {
						loginInfo = result;
						if (loginInfo.isLoggedIn()) {
							GWT.log("success");
						} else {
							GWT.log("failed");
						}
					}
				});
		// service.saveVideo(video, new AsyncCallback<Video>(){

		// @Override
		// public void onFailure(Throwable caught) {
		// GWT.log("failed");
		//
		// }
		//
		// @Override
		// public void onSuccess(Video result) {
		// GWT.log("success");
		//
		// }});

	}

}
